package com.wuxu.lib_skin_core.load

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import com.wuxu.lib_skin_core.constant.SKIN_APK_TYPE
import com.wuxu.lib_skin_core.constant.SKIN_INNER_TYPE
import com.wuxu.lib_skin_core.res.ResourceSingleton
import com.wuxu.lib_skin_core.res.SkinManager
import com.wuxu.lib_skin_core.utils.SkinApkUtils

/**
 *@author wuxu
 *
 * 策略接口
 */
interface SkinLoadStrategy {

    /**
     * 后台加载皮肤
     *
     *@param
     *@return String  加载成功返回皮肤包名称，加载失败返回null
     */
    fun loadSkinInBackground(context: Context, skinName:String): String?

    /**
     * 获取资源名
     *
     *@param context，皮肤包名称
     *@return 资源名称
    */
    fun getResourceEntryName(context: Context, skinName:String,resId:Int):String?
}


/**
 * @author wuxu
 *
 * 应用内换肤加载策略
 */
class SkinInnerLoadStrategy : SkinLoadStrategy {
    override fun loadSkinInBackground(context: Context,skinName:String): String? {
        ResourceSingleton.setupSkin(context.resources,context.packageName,skinName,this)
        SkinManager.notifySkinUpdate()
        return skinName
    }

    override fun getResourceEntryName(context: Context, skinName: String, resId: Int) :String{
        return "${ResourceSingleton.getResource()?.getResourceEntryName(resId)}_$skinName"
    }

}

/**
 * @author wuxu
 *
 * APK换肤加载策略
 */
class SkinApkLoadStrategy : SkinLoadStrategy {
    override fun loadSkinInBackground(context: Context,skinName:String): String? {
        SkinApkUtils.getSkinApkInfo(context,skinName)?.let {apkInfo->
            ResourceSingleton.setupSkin(apkInfo.resources,apkInfo.packageName,skinName,this)
            Log.i("wuxu","apkInfo: $apkInfo")
        }
        return skinName
    }

    override fun getResourceEntryName(context: Context, skinName: String, resId: Int):String?{
        return ResourceSingleton.getResource()?.getResourceEntryName(resId)
    }
}

/**
 * 默认策略，什么都不处理
 */
class SkinNoneLoadStrategy : SkinLoadStrategy {
    override fun loadSkinInBackground(context: Context,skinName:String): String? {
        return null
    }

    override fun getResourceEntryName(context: Context, skinName: String, resId: Int) =null

}

class SkinLoader(val skinLoadStrategy: SkinLoadStrategy) {

}


object SkinLoadStrategyFactory {
    private val mSkinLoadStrategy = HashMap<Byte, SkinLoadStrategy>()

    /**
     * 采用工厂模式+享元模式，避免频繁创建相同的策略对象
     */
    fun getSkinLoadStrategy(skinStrategyType: Byte): SkinLoadStrategy {
        var skinLoadStrategy = mSkinLoadStrategy[skinStrategyType]
        if (skinLoadStrategy == null) {
            skinLoadStrategy = createSkinLoadStrategy(skinStrategyType)
            mSkinLoadStrategy[skinStrategyType] = skinLoadStrategy
        }
        return skinLoadStrategy
    }

    private fun createSkinLoadStrategy(skinStrategyType: Byte): SkinLoadStrategy = when(skinStrategyType){
        SKIN_INNER_TYPE->SkinInnerLoadStrategy()
        SKIN_APK_TYPE->SkinApkLoadStrategy()
        else -> SkinNoneLoadStrategy()
    }
}